import { keydownCondPairs } from '../common.js';
// import { confirmModalProps, judgeModalProps } from './use-judge.js';
import { kbdModalProps } from '../kbd-modal.js';
// import { labelSelectorProps } from './use-layer.js';

const typing = () => R.anyPass([R.is(HTMLInputElement), R.is(HTMLTextAreaElement)])(document.activeElement);

const needPrevent = R.cond([
  [typing,                              () => false],
  // [() => labelSelectorProps.value.visible, () => false],
  // [() => judgeModalProps.value.visible, () => false],
  [() => kbdModalProps.value.visible,   () => false],
  [event => 'F5' == event.key,          () => false],
  [event => 'F12' == event.key,         () => false],
  [event => 'F11' == event.key,         () => false],
  [() => true,                          () => true],
]);

function allowCondPairs() {
  return !typing()
    // && !labelSelectorProps.value.visible
    // && !judgeModalProps.value.visible
    && !kbdModalProps.value.visible
    // && !confirmModalProps.value.visible;
}

export const handleShortcut = event => {
  needPrevent(event) && event.preventDefault();
  allowCondPairs() && R.cond(keydownCondPairs)(event);
  listenKeydown();
}

export function listenKeydown() {
  // 浏览器快捷键 keydown 触发，故在 keydown 调 preventDefault()
  // 部分快捷键需要阻止，故 preventDefault() 前加判断
  // 编辑输入框时取消触发快捷键
  addEventListener('keydown', handleShortcut, { once: true });
};
